package com.qiantu.phone.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.hjq.shape.layout.ShapeFrameLayout;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.phone.R;
import com.qiantu.phone.ui.activity.HomeActivity;
import com.qiantu.phone.ui.adapter.EnviromentScrollAdapter;

import java.util.List;

public class HomeEnviromentView extends ShapeFrameLayout {
    private RecyclerView enviromentRecyclerView;
    private EnviromentScrollAdapter mEnviromentAdapter;
    private boolean hasScene;
    /**
     * 0 默认 1常用 2通用
     */
    private int type;
    //场景
//    private List<SceneBean> scenes = null;
    private String roomSerialNo;
    private int marginLeftRight;
    private int shadowSize;
    private int marginTop;

    public HomeEnviromentView(Context context, int type, String roomSerialNo) {
        this(context, null, type, roomSerialNo);
    }

    public HomeEnviromentView(Context context, AttributeSet attrs, int type, String roomSerialNo) {
        this(context, attrs, 0, type, roomSerialNo);
    }

    public HomeEnviromentView(Context context, AttributeSet attrs, int defStyleAttr, int type, String roomSerialNo) {
        super(context, attrs, defStyleAttr);
        this.type = type;
        this.roomSerialNo = roomSerialNo;
        StaggeredGridLayoutManager.LayoutParams layoutParams = new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.bottomMargin = getResources().getDimensionPixelSize(R.dimen.dp_8);
        layoutParams.height = 1;
        setLayoutParams(layoutParams);
        //由于当前view会添加到RecyclerView里，让setOnItemLongClickListener无效
        setEnabled(false);
        initView();
    }

    protected void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.home_enviroment_layout, this, true);
        enviromentRecyclerView = view.findViewById(R.id.environment_recycler_view);
        mEnviromentAdapter = new EnviromentScrollAdapter(getContext());
        enviromentRecyclerView.setAdapter(mEnviromentAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        enviromentRecyclerView.setLayoutManager(linearLayoutManager);
    }

    public void refreshSceneData() {

    }

    public void updateView(List<DeviceBean> scenes) {
        StaggeredGridLayoutManager.LayoutParams sl = (StaggeredGridLayoutManager.LayoutParams) getLayoutParams();
        if (scenes != null && scenes.size() > 0) {
            enviromentRecyclerView.setVisibility(View.VISIBLE);
            mEnviromentAdapter.setData(scenes);
            hasScene = true;
            sl.height = StaggeredGridLayoutManager.LayoutParams.WRAP_CONTENT;;
        } else {
            enviromentRecyclerView.setVisibility(View.GONE);
            mEnviromentAdapter.setData(null);
            sl.height = 1;
            hasScene = false;
        }
        sl.bottomMargin = 0;
        setPadding(0, 0, 0, 0);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
//        if (getContext() instanceof HomeActivity) {
//            boolean hasFloor = ((HomeActivity) getContext()).getHomeFragment().hasFloor();
//            StaggeredGridLayoutManager.LayoutParams sl = (StaggeredGridLayoutManager.LayoutParams) getLayoutParams();
//            if (hasFloor) {
//                //不是常用和通用
//                if (type != 1 && type != 2) {
//                    int topMargin = ((HomeActivity) getContext()).getHomeFragment().getSubFragmentTop();
//                    if (sl.topMargin != topMargin) {
//                        sl.topMargin = topMargin;
//                    }
//                }
//            } else {
//                sl.topMargin = marginTop;
//            }
//        }
    }

    public boolean hasScene() {
        return hasScene;
    }
}
